
//load data
function loadDom3Data(fn) {

	var filestoload = [
		'gamedata/BaseI.csv'+versionCode,
		'gamedata/BaseA.csv'+versionCode,
//		'gamedata/BaseU.csv'+versionCode,
		'gamedata/BaseW.csv'+versionCode
	];

	//read mod urls from query string
	var modstoload = (new ParsedQueryString()).params("mod");
	
	preloadData( filestoload.concat(modstoload), function(dataobj) {
			//all loaded.. parse tables			
			var data = dataobj['gamedata/BaseI.csv'+versionCode];
			if (!data) throw(error('Failed to load file: gamedata/BaseI.csv'));
			modctx.itemdata = parseTextToTable(data);
			modctx.itemlookup = createLookup(modctx.itemdata, 'id', 'name');
			
			var data = dataobj['gamedata/BaseA.csv'+versionCode];
			if (!data) throw(error('Failed to load file: gamedata/BaseA.csv'));
			modctx.armordata = parseTextToTable(data, 'option_ignore0');
			modctx.armorlookup = createLookup(modctx.armordata, 'id', 'armorname');
	
//			var data = dataobj['gamedata/BaseU.csv'+versionCode];
//			if (!data) throw(error('Failed to load file: gamedata/BaseU.csv'));
//			modctx.unitdata = parseTextToTable(data);
//			modctx.unitlookup = createLookup(unitdata, 'id', 'name');
	
			var data = dataobj['gamedata/BaseW.csv'+versionCode];
			if (!data) throw(error('Failed to load file: gamedata/BaseW.csv'));
			modctx.wpndata = parseTextToTable(data, 'option_ignore0');
			modctx.wpnlookup = createLookup(modctx.wpndata, 'id', 'wpname');
			
			
			//before applying mod
			prepareItemData_PreMod();
	
			//parse the mods
			for (var i=0; i<modstoload.length; i++) {
				var data = dataobj[modstoload[i]];
				if (data)
					parseMod( data );
				else
					error('Failed to mod: '+modstoload[i])
			}
			
			//after applying mod
			prepareItemData_PostMod();

			//run callback
			fn();
	});
}


function preloadData(urllist, onfinish) {
	if (urllist.length == 0) {
		onfinish({});
		return;
	}
	//outer closure
	var loadedData = {};
	var numFinished = 0;
	var onfinish = onfinish;
	
	for (var i=0; i<urllist.length; i++) {
		//inner closure for each url
		var fn = function(){
			var url = urllist[i];
			$.ajax({
				url: url,
				dataType:  'text',
				success: function(data) {
					loadedData[url] = data;
					if (++numFinished == urllist.length)
						onfinish(loadedData);
				},
				error: function(wtf, textStatus, errorThrown) {
					console.log("Error loading file: " + url 
						+ '\n>'+ textStatus 
						+ '\n>' + JSON.stringify(wtf)
						+ '\n>' + JSON.stringify(errorThrown)
					);
					loadedData[url] = undefined;
					if (++numFinished == urllist.length)
						onfinish(loadedData);
				}
			});
		};fn();
	}
}



function parseTextToTable(str, option_ignore0) {
	var t = [];
	
	var lines = str.split("\n");
	var keynames = lines[0].split("\t");

	for (var i=1; i<lines.length; i++) {
		var values = lines[i].split("\t");
		
		if (values[0]=="")
			continue;
		
		var o = new Object();
		for (var j=0; j<keynames.length; j++) {
			var key = keynames[j];
			var val = values[j]
			if (val != "" && val != "\r" && !(option_ignore0 && val=='0')) {
				if (key == 'id#') key = 'id';
				o[key] = values[j];
			}
		}
		t.push(o);	 
	}	
	return t;
}

function createLookup(t, k1, k2) {
	var o = {};
	for (var i=0; i<t.length; i++) {
		var line = t[i];
		o[line[k1]] = line;
		if (k2) 
			o[line[k2]] = line;
	}
	return o;
}
